package com.sctvcloud.dongqu.ui.adapter;

import android.content.Context;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ruihang.generalibrary.ui.adapter.BaseHolderAbsAdapter;
import com.ruihang.generalibrary.ui.util.IAndroidLittleLife;
import com.ruihang.generalibrary.ui.util.LittleLifeManager;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.base.BaseActivity;
import com.sctvcloud.dongqu.base.BaseFragment;
import com.sctvcloud.dongqu.ui.adapter.holder.BaseAbsHolder;
import com.sctvcloud.dongqu.ui.adapter.holder.GovLivelihoodHolder;
import com.sctvcloud.dongqu.ui.adapter.holder.GovMailHolder;
import com.sctvcloud.dongqu.ui.adapter.holder.GovMentHolder;

import com.sctvcloud.dongqu.ui.adapter.holder.GovPolicyTitleHolder;
import com.sctvcloud.dongqu.ui.adapter.holder.News24Holder;
import com.sctvcloud.dongqu.ui.util.IListShowData;

import java.util.ArrayList;
import java.util.List;

/**
 * 政务民声
 */
public class CivilServiceAdapter extends BaseHolderAbsAdapter<IListShowData, BaseAbsHolder<IListShowData>> {

    public static final int VIEWTYPE_MAIL = 1;//信箱
    public static final int VIEWTYPE_GOVERNMENT = 2;//政务大厅
    public static final int VIEWTYPE_LIVELIHOOD = 3;//民生爆料
    public static final int VIEWTYPE_POLICY_TITLE = 5;//政策解读 title
    public static final int VIEWTYPE_POLICY = 6;//政策解读
    private GovLivelihoodHolder.OnGovLivelihoodClickistener livelihoodClicklistener;
    private LittleLifeManager littleLifeManager;

    private ArrayList<Pair<String, String>> pairs;

    public void setPairs(ArrayList<Pair<String, String>> pairs) {
        this.pairs = pairs;
    }

    public CivilServiceAdapter(Context context, List list) {
        super(context, list);
        littleLifeManager = new LittleLifeManager();
    }

    /**
     * 民生爆料 点击监听
     *
     * @param livelihoodClicklistener
     */
    public void setLivelihoodClicklistener(GovLivelihoodHolder.OnGovLivelihoodClickistener livelihoodClicklistener) {
        this.livelihoodClicklistener = livelihoodClicklistener;
    }


    @Override
    public BaseAbsHolder<IListShowData> onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseAbsHolder bah = null;
        View v = null;
        switch (viewType) {
            case VIEWTYPE_MAIL://信箱
                v = LayoutInflater.from(context).inflate(R.layout.item_gov_mail, parent, false);
                bah = new GovMailHolder(context, v);
                bah.setInternalClick(this);
                break;
            case VIEWTYPE_GOVERNMENT://政务大厅
                v = LayoutInflater.from(context).inflate(R.layout.item_government_hall, parent, false);
                bah = new GovMentHolder(context, v);
                break;
            case VIEWTYPE_LIVELIHOOD://民生爆料
                v = LayoutInflater.from(context).inflate(R.layout.item_liveli_hood, parent, false);
                bah = new GovLivelihoodHolder(context, v, livelihoodClicklistener);
                break;
            case VIEWTYPE_POLICY_TITLE:// 政策解读 title
                v = LayoutInflater.from(context).inflate(R.layout.item_gov_policy_title, parent, false);
                bah = new GovPolicyTitleHolder(context, v);
                break;
            case VIEWTYPE_POLICY:// 政策解读
            default:
                v = LayoutInflater.from(context).inflate(R.layout.item_news_24_cl, parent, false);
                bah = new News24Holder(context, v);
                break;
        }
//        bah.setActivity(activity);
//        bah.setFragment(fragment);
        bah.setPairs(pairs);
        return bah;
    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position).getDataViewType();
    }

    @Override
    public void onBindViewHolder(BaseAbsHolder<IListShowData> holder, int position) {
        super.onBindViewHolder(holder, position);
        holder.setItemMode(position == getItemCount() - 1 ? BaseAbsHolder.ITEMMODE_LAST : BaseAbsHolder.ITEMMODE_NORMAL);
    }

    @Override
    public void onResume() {
        littleLifeManager.onResume();
    }

    @Override
    public void onPause() {
        littleLifeManager.onPause();
    }

    @Override
    public void onDestory() {
        littleLifeManager.onDestory();
    }

    protected void addLife(IAndroidLittleLife ia) {
        littleLifeManager.addLifeInf(ia);
    }


    @Override
    public CivilServiceAdapter setItemInternalClick(OnItemInternalClick itemInternalClick) {
        super.setItemInternalClick(itemInternalClick);
        return this;
    }

    @Override
    public void addDatas(List<IListShowData> list) {
        super.addDatas(list);
    }

    public void resetDatasFroms(int pos, List<? extends IListShowData> datas, boolean isForceAdd) {
        if (datas == null || datas.size() == 0) {
            return;
        }
        if (pos < 0) {
            if (isForceAdd) {
                addDatass(datas);
            }
        } else {
            int count = getItemCount();
            if (pos < count) {
                data = data.subList(0, pos);
//                JLog.e("after sub size is " + data.size());
                data.addAll(datas);
                notifyItemRangeChanged(pos, Math.max(count - pos, datas.size()));
            } else {
                addDatass(datas);
            }
        }
    }

    public void addDatass(List<? extends IListShowData> list) {
        if (data == null) {
            data = new ArrayList<>();
            data.addAll(list);
            if (data.size() > 0) {
                notifyDataSetChanged();
            }
        } else if (list != null && list.size() > 0) {
            int fromPos = getItemCount();
            if (fromPos < 0) {
                fromPos = 0;
            }
            data.addAll(list);
            notifyItemRangeChanged(fromPos, list.size());
        }
    }

    public void setDatass(List<? extends IListShowData> list) {
        data.clear();
        data.addAll(list);
        notifyDataSetChanged();
    }


}
